package com.xzq.erp.controller;

import com.xzq.erp.aspect.ApiOperationLog;
import com.xzq.erp.domain.dto.ComplaintReplyDTO;
import com.xzq.erp.domain.dto.PageDTO;
import com.xzq.erp.domain.query.ComplaintReplyPageQuery;
import com.xzq.erp.domain.vo.ComplaintReplyVO;
import com.xzq.erp.service.IComplaintReplyService;
import com.xzq.erp.utils.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 投诉回复表 前端控制器
 * </p>
 *
 * @author xzq
 * @since 2025-05-13
 */
@RestController
@RequestMapping("/complaint-reply")
@Tag(name = "投诉回复相关接口")
@RequiredArgsConstructor
@Validated
public class ComplaintReplyController {

    private final IComplaintReplyService complaintReplyService;

    /**
     * 分页查询投诉回复列表
     */
    @GetMapping("/list")
    @Operation(summary = "分页查询投诉回复列表")
    @ApiOperationLog(description = "分页查询投诉回复列表")
    @PreAuthorize("hasAuthority('complaintReply:list') || hasRole('ADMIN')")
    public Response<PageDTO<ComplaintReplyVO>> getComplaintReplyList(@Validated @ParameterObject ComplaintReplyPageQuery query) {
        return Response.success(complaintReplyService.getComplaintReplyList(query));
    }

    /**
     * 根据售后服务ID获取回复列表
     */
    @GetMapping("/by-after-sales/{afterSalesId}")
    @Operation(summary = "根据售后服务ID获取回复列表")
    @ApiOperationLog(description = "根据售后服务ID获取回复列表")
    @PreAuthorize("hasAuthority('complaintReply:list') || hasRole('ADMIN')")
    public Response<List<ComplaintReplyVO>> getReplyListByAfterSalesId(
            @PathVariable @Parameter(description = "售后服务ID") Long afterSalesId) {
        return Response.success(complaintReplyService.getReplyListByAfterSalesId(afterSalesId));
    }

    /**
     * 添加投诉回复
     */
    @PostMapping
    @Operation(summary = "添加投诉回复")
    @ApiOperationLog(description = "添加投诉回复")
    @PreAuthorize("hasAuthority('complaintReply:add') || hasRole('ADMIN')")
    public Response<Void> addComplaintReply(@RequestBody @Validated ComplaintReplyDTO replyDTO) {
        complaintReplyService.addComplaintReply(replyDTO);
        return Response.success();
    }

    /**
     * 修改投诉回复
     */
    @PutMapping
    @Operation(summary = "修改投诉回复")
    @ApiOperationLog(description = "修改投诉回复")
    @PreAuthorize("hasAuthority('complaintReply:update') || hasRole('ADMIN')")
    public Response<Void> updateComplaintReply(@RequestBody @Validated ComplaintReplyDTO replyDTO) {
        complaintReplyService.updateComplaintReply(replyDTO);
        return Response.success();
    }

    /**
     * 删除投诉回复
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除投诉回复")
    @ApiOperationLog(description = "删除投诉回复")
    @PreAuthorize("hasAuthority('complaintReply:delete') || hasRole('ADMIN')")
    public Response<Void> deleteComplaintReply(
            @PathVariable @Parameter(description = "回复ID") Long id) {
        complaintReplyService.deleteComplaintReply(id);
        return Response.success();
    }

    /**
     * 提交满意度评价
     */
    @PutMapping("/{id}/satisfaction")
    @Operation(summary = "提交满意度评价")
    @ApiOperationLog(description = "提交满意度评价")
    @PreAuthorize("hasAuthority('complaintReply:update') || hasRole('ADMIN')")
    public Response<Void> submitSatisfactionRating(
            @PathVariable @Parameter(description = "回复ID") Long id,
            @RequestParam @Parameter(description = "满意度评分(1-5分)") Integer rating) {
        complaintReplyService.submitSatisfactionRating(id, rating);
        return Response.success();
    }
}
